﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using ZHDJ.Core.Util;

namespace ZHDJ.Core
{
    public class UploadFileHelp
    {
        #region 读取excel
        /// <summary>
        /// 读出excel数据到dataset
        /// </summary>
        /// <param name="fuFile"></param>
        /// <param name="strSheetName"></param>
        /// <returns></returns>
        public static DataSet ImportToDataSet(FileUpload fuFile, string strSheetName)
        {
            System.Data.DataSet ds = null;
            if (!string.IsNullOrEmpty(fuFile.FileName))
            {
                string strExt = fuFile.FileName.Substring(fuFile.FileName.LastIndexOf(".") + 1);
                string strFileFullPath = UploadFileToTempDirectory(fuFile); //上传临时文件
                if (!string.IsNullOrEmpty(strFileFullPath))
                {
                    string selectConnectionString = null;
                    //if (strExt == "xlsx")
                    //{
                    selectConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;", strFileFullPath);
                    //}
                    //else
                    //{
                    //    selectConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", strFileFullPath);
                    //}

                    string strCommandText = string.Format("select * from [{0}$]", strSheetName);
                    OleDbDataAdapter oDataAdapter = new OleDbDataAdapter(strCommandText, selectConnectionString);
                    ds = new DataSet();
                    oDataAdapter.Fill(ds);
                    //获取完数据后删除临时文件
                    DeleteTempFile(strFileFullPath);
                }
            }
            return ds;
        }

        /// <summary>
        /// 读出excel数据到dataset
        /// </summary>
        /// <param name="fuFile"></param>
        /// <param name="strSheetName"></param>
        /// <returns></returns>
        public static DataSet ImportToDataSetEx(FileUpload fuFile, string strSheetName)
        {
            System.Data.DataSet ds = null;
            //if (!string.IsNullOrEmpty(fuFile.FileName))
            //{
            //    string strExt = fuFile.FileName.Substring(fuFile.FileName.LastIndexOf(".") + 1);
            //    string strFileFullPath = UploadFileToTempDirectory(fuFile); //上传临时文件
            //    if (!string.IsNullOrEmpty(strFileFullPath))
            //    {
            //        Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(strFileFullPath);
            //        Cells cells = wb.Worksheets[0].Cells;
            //        DataTable dt = cells.ExportDataTable(0, 0, cells.MaxDataRow + 1, cells.MaxColumn + 10);
            //        ds = new DataSet();
            //        ds.Tables.Add(dt);
            //        //获取完数据后删除临时文件
            //        DeleteTempFile(strFileFullPath);
            //    }
            //}
            return ds;
        }


        private static string UploadFileToTempDirectory(FileUpload fuFile)
        {
            string uploadTempDirectory = UploadTempDirectory;
            string str2 = fuFile.FileName.Substring(fuFile.FileName.LastIndexOf("."));
            uploadTempDirectory = uploadTempDirectory + StringHelper.NewID + str2;
            if (CreateTempDirectory(uploadTempDirectory))
            {
                fuFile.SaveAs(uploadTempDirectory);
                return uploadTempDirectory;
            }
            return "";
        }
        private static string UploadTempDirectory
        {
            get
            {
                string str = HttpContext.Current.Server.MapPath("~/");
                if (!str.EndsWith(@"\"))
                {
                    str = str + @"\";
                }
                return (str + @"FileUploadTemp\");
            }
        }

        private static bool CreateTempDirectory(string fileFullPath)
        {
            try
            {
                FileInfo info = new FileInfo(fileFullPath);
                DirectoryInfo info2 = new DirectoryInfo(info.DirectoryName);
                if (!info2.Exists)
                {
                    info2.Create();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private static bool DeleteTempFile(string fileFullPath)
        {
            try
            {
                new FileInfo(fileFullPath).Delete();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        #endregion
    }
}
